package com.bw.servlet;

import com.bw.util.JDBC;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/***************************
 FileAction:

 CreateTime:2022-08-19 15:29
 Author:Aaking-aline
 ****************************/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	@SneakyThrows
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");


		Connection conn = JDBC.getConnecton();
		PreparedStatement ps = conn.prepareStatement("select * from user where username=? and password=?");
		ps.setObject(1,username);
		ps.setObject(2,password);
		ResultSet rs = ps.executeQuery();

		if(rs.next()){
			response.sendRedirect("message.html");
			return ;
		}
		response.sendRedirect("login.html");
	}
}
